#### Getting sufficient libraries / ���������� ����������� ������ ####

library(readr)
library(readxl)
library(openxlsx)
library(igraph)
library(igraphdata)
library(sqldf)

#### Macro-networks ####

#### 3.2.1. Networks of twin- and partner-cities and regions / ���� ����������� � ���������� � �������� � ���������

net_nod$Class <- as.factor(net_nod$Class)
net_nod$Type <- as.factor(net_nod$Type)
net_nod$Stage <- as.factor(net_nod$Stage)
net_macro <- graph_from_data_frame(d=net_edg, vertices=net_nod, directed=F)

coul_d <- c("orange","red2","dodgerblue4",'forestgreen')
my_color_d <- coul_d[as.numeric(as.factor(V(net_macro)$Class))]
V(net_macro)$label.color="black"
V(net_macro)$shape= 'circle'
V(net_macro)$shape= ifelse(V(net_macro)$Type == 'region', "square", V(net_macro)$shape)
V(net_macro)$size=7
V(net_macro)$color=my_color_d
E(net_macro)$width=net_edg$Weight*1.5
E(net_macro)$color='grey27'

set.seed(500)
plot(net_macro, layout=layout_nicely)

#### 3.2.1.a Networks of twin- and partner-cities and regions (by gubernators) / ���� ����������� � ���������� (�� ������������)

net_macro1 <- graph_from_data_frame(d=net_edg, vertices=net_nod, directed=F)

coul_d1 <- c('chartreuse',"dodgerblue4","orange",'forestgreen','darkorchid3',"red2",'deeppink1')
my_color_d1 <- coul_d1[as.numeric(as.factor(V(net_macro1)$Stage))]
V(net_macro1)$label.color="black"
V(net_macro1)$shape= 'circle'
V(net_macro1)$shape= ifelse(V(net_macro1)$Type == 'region', "square", V(net_macro1)$shape)
V(net_macro1)$size=7
V(net_macro1)$color=my_color_d1
E(net_macro1)$width=net_edg$Weight*1.5
E(net_macro1)$color='grey27'

set.seed(500)
plot(net_macro1, layout=layout_nicely)

#### 3.2.2. Networks of SPB districts and municipalities with foreign counterparts / ���� ���������������� ������� � ������������� ����������� � ����������� �������� � ���������

e_spbd$Type <- as.factor(e_spbd$Type)
n_spbd$Type <- as.factor(n_spbd$Type)

net_spbd <- graph_from_data_frame(d=e_spbd, vertices=n_spbd, directed=F)

coul <- c("lightblue2","red2","dodgerblue4",'forestgreen','salmon1')
my_color <- coul[as.numeric(as.factor(E(net_spbd)$Type))]

V(net_spbd)$label.color='black'
V(net_spbd)$color='gold'
V(net_spbd)$size=6.3
V(net_spbd)$vertex.label=n_spbd$Id
V(net_spbd)$shape="circle"
V(net_spbd)$shape = ifelse(V(net_spbd)$Type == 'region', "sphere", V(net_spbd)$shape)
V(net_spbd)$shape = ifelse(V(net_spbd)$Type == 'district', "square", V(net_spbd)$shape)
E(net_spbd)$width=3.5
E(net_spbd)$color=my_color
E(net_spbd)$lty='solid'
E(net_spbd)$curved=0

set.seed(500)
plot(net_spbd, layout=layout_nicely)

### Networks of SPB districts and municipalities with foreign counterparts (communities) / ���� ���������������� ������� � ������������� ����������� � ����������� �������� � ��������� (����������)

ceb_net_spbd <- cluster_edge_betweenness(net_spbd)
dendPlot(ceb_net_spbd,mode='hclust')
class(ceb_net_spbd)
length(ceb_net_spbd)
membership(ceb_net_spbd)
modularity(ceb_net_spbd)
set.seed(500)
plot(ceb_net_spbd, net_spbd)

#### Micro-networks ####

#### 3.3.1. Phenomenon of negative ties: case of SPB / ������� ���������� ������ �� ������� �����-����������.

spb_nodes_b2022$Type <- as.factor(spb_nodes_b2022$Type)
spb_edges_b2022$Type <- as.factor(spb_edges_b2022$Type)

net_neg_spb <- graph_from_data_frame(d=spb_edges_b2022, vertices=spb_nodes_b2022, directed=F)

coul2 <- c("lightblue1","red2")
my_color2 <- coul2[as.numeric(as.factor(V(net_neg_spb)$Type))]
V(net_neg_spb)$label.color="black"
V(net_neg_spb)$vertex.label=spb_nodes_b2022$Id
V(net_neg_spb)$color= my_color
V(net_neg_spb)$size=13
E(net_neg_spb)$width=1.8
E(net_neg_spb)$color=c('gray26')

set.seed(500)
plot(net_neg_spb, layout=layout_nicely)

#### 3.3.2. Emergent communicative networks based on news / ������������ ��������������� ���� �� ������ �������� 

ek_net19 <- graph_from_data_frame(d=e2019, vertices=n2019, directed=F)
ek_net19 <- simplify(ek_net19, remove.loops = T)
V(ek_net19)$size=2
V(ek_net19)$label=NA
V(ek_net19)$label.color='grey15'
V(ek_net19)$label.dist=0.2
V(ek_net19)$color='orange'
V(ek_net19)$frame.color='blue3'
E(ek_net19)$color='red3'
set.seed(500)
plot(ek_net19, layout=layout_with_fr)

ek_net20 <- graph_from_data_frame(d=e2020, vertices=n2020, directed=F)
ek_net20 <- simplify(ek_net20, remove.loops = T)
V(ek_net20)$size=2
V(ek_net20)$label=NA
V(ek_net20)$label.color='grey15'
V(ek_net20)$label.dist=0.2
V(ek_net20)$color='orange'
V(ek_net20)$frame.color='blue3'
E(ek_net20)$color='red3'
set.seed(500)
plot(ek_net20, layout=layout_with_kk)

ek_net21 <- graph_from_data_frame(d=e2021, vertices=n2021, directed=F)
ek_net21 <- simplify(ek_net21, remove.loops = T)
V(ek_net21)$size=2
V(ek_net21)$label=NA
V(ek_net21)$label.color='grey15'
V(ek_net21)$label.dist=0.2
V(ek_net21)$color='orange'
V(ek_net21)$frame.color='blue3'
E(ek_net21)$color='red3'
set.seed(500)
plot(ek_net21, layout=layout_with_fr)

ek_net22 <- graph_from_data_frame(d=e2022, vertices=n2022, directed=F)
ek_net22 <- simplify(ek_net22, remove.loops = T)
V(ek_net22)$size=2
V(ek_net22)$label=NA
V(ek_net22)$label.color='grey15'
V(ek_net22)$label.dist=0.2
V(ek_net22)$color='orange'
V(ek_net22)$frame.color='blue3'
E(ek_net22)$color='red3'
set.seed(500)
plot(ek_net22, layout=layout_with_fr)

### Strong (twin) ties of SPB with other foreign cities / ������� (������������) ����� �����-���������� � ������� ����������� ��������

clean_graph19 <- graph_from_data_frame(d=clean_edges19, vertices=clean_nodes19, directed=F)
clean_graph19 <- simplify(clean_graph19, remove.loops = T)
V(clean_graph19)$size=7.5
V(clean_graph19)$label.color='grey15'
V(clean_graph19)$label.dist=0
V(clean_graph19)$shape='circle'
V(clean_graph19)$shape = ifelse(V(clean_graph19)$Type == 'region', "square", V(clean_graph19)$shape)
V(clean_graph19)$color='orange'
V(clean_graph19)$frame.color='blue3'
E(clean_graph19)$color='red3'
set.seed(500)
plot(clean_graph19, layout=layout_with_fr)

clean_graph20 <- graph_from_data_frame(d=clean_edges20, vertices=clean_nodes20, directed=F)
clean_graph20 <- simplify(clean_graph20, remove.loops = T)
V(clean_graph20)$size=7.5
V(clean_graph20)$label.color='grey15'
V(clean_graph20)$label.dist=0
V(clean_graph20)$shape='circle'
V(clean_graph20)$shape = ifelse(V(clean_graph20)$Type == 'region', "square", V(clean_graph20)$shape)
V(clean_graph20)$color='orange'
V(clean_graph20)$frame.color='blue3'
E(clean_graph20)$color='red3'
set.seed(500)
plot(clean_graph20, layout=layout_with_fr)

clean_graph21 <- graph_from_data_frame(d=clean_edges21, vertices=clean_nodes21, directed=F)
clean_graph21 <- simplify(clean_graph21, remove.loops = T)
V(clean_graph21)$size=7.5
V(clean_graph21)$label.color='grey15'
V(clean_graph21)$label.dist=0
V(clean_graph21)$shape='circle'
V(clean_graph21)$shape = ifelse(V(clean_graph21)$Type == 'region', "square", V(clean_graph21)$shape)
V(clean_graph21)$color='orange'
V(clean_graph21)$frame.color='blue3'
E(clean_graph21)$color='red3'
set.seed(500)
plot(clean_graph21, layout=layout_with_fr)

clean_graph22 <- graph_from_data_frame(d=clean_edges22, vertices=clean_nodes22, directed=F)
clean_graph22 <- simplify(clean_graph22, remove.loops = T)
V(clean_graph22)$size=7.5
V(clean_graph22)$label.color='grey15'
V(clean_graph22)$label.dist=0
V(clean_graph22)$shape='circle'
V(clean_graph22)$shape = ifelse(V(clean_graph22)$Type == 'region', "square", V(clean_graph22)$shape)
V(clean_graph22)$color='orange'
V(clean_graph22)$frame.color='blue3'
E(clean_graph22)$color='red3'
set.seed(500)
plot(clean_graph22, layout=layout_with_fr)

par(mfrow=c(2,2))
set.seed(500)
plot(clean_graph19, layout=layout_with_fr, main='2019 ���')
set.seed(500)
plot(clean_graph20, layout=layout_with_fr, main='2020 ���')
set.seed(500)
plot(clean_graph21, layout=layout_with_fr, main='2021 ���')
set.seed(500)
plot(clean_graph22, layout=layout_with_fr, main='2022 ���')



